package com.example.day45.animator;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author zhangwei
 * @version V1.0
 * @Title: Advanced
 * @Package com.example.day45.animator
 * @Description:
 * @date 15/10/29 下午5:24
 */

public class DrawLineView extends View {
    private int startX = 50;
    private Paint paint = new Paint();
    private int startY = 50;
    private int endX = 50;
    private int endY = 50;
        
    public DrawLineView(Context context) {
        super(context);
        paint.setColor(Color.RED);
        paint.setTextSize(30);
    }

    public DrawLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public DrawLineView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(startX, startY, endX, endY, paint);
        ValueAnimator animator = ValueAnimator.ofInt(300);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedFraction = (int) animation.getAnimatedFraction();
                setEndX(endY + animatedFraction);
                invalidate();
            }
        });
    }


    public void setEndX(int endX) {
        this.endX = endX;
    }

    public void setEndY(int endY) {
        this.endY = endY;
    }
}
